using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.LogServices.IServices;
using IM.Easy.Entity.Log;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// RequestTrackingLogController 控制器
    /// </summary>
    [Route("log/requesttrackinglog")]
    public class RequestTrackingLogController : ControllerBase
    {
        private readonly IRequestTrackingService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public RequestTrackingLogController(IRequestTrackingService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有RequestTrackingLog对象
        /// </summary>
        /// <returns>返回RequestTrackingLog列表</returns>
        [Api(Name = "获取操作记录列表", Perm = "operation:list")]
        [HttpGet]
        public PageResponse<RequestTrackingLog> GetList([FromQuery] RequestTrackingLogQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取RequestTrackingLog对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的RequestTrackingLog对象</returns>
        [Api(Name = "获取操作记录明细", Perm = "operation:info")]
        [HttpGet("{id}")]
        public RequestTrackingLog GetById([FromRoute] string id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的RequestTrackingLog对象。");
        }

        /// <summary>
        /// 批量删除RequestTrackingLog对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [Api(Name = "删除操作记录", Perm = "operation:del")]
        [HttpDelete]
        public void Delete([FromBody] Guid[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 一键清空所有
        /// </summary>
        /// <returns></returns>
        [Api(Name = "操作记录一键清空", Perm = "operation:del:all")]
        [HttpDelete("clear")]
        public bool ClearAll()
        {
            return _service.Delete(s => true);
        }

        /// <summary>
        /// 获取操作记录统计数据集
        /// </summary>
        [Api(Name = "获取操作记录数据集", AllowAnonymous = true, TrackLog = false)]
        [HttpGet("chart")]
        public List<OperationChartData> ChartDatasets()
        {
            var query = new RequestTrackingLogQueryDto();
            var data = _service.GetDateData(query);
            return data;
        }
    }
}
